﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.DBM; 

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class DC_DisciplineCommandPopup : System.Web.UI.UserControl
    {

        Command_DisciplineBLL obj = new Command_DisciplineBLL(); 
     
        private object _dataItem = null;

        protected void Page_Load(object sender, EventArgs e)
       
        
        {

          
            

        }



        protected bool IsUserControlPostBack
        {
            get
            {
                return this.ViewState["IsUserControlPostBack"] != null;
            }
        }

  
        protected override void OnLoad(System.EventArgs e)
        {
            base.OnLoad(e);

          


            if (!IsUserControlPostBack)
            {

                BindYear();
                BindComboEmpType();
                BindComboInvestigation();
                BindComboPenaltyLevel();
                BindComboCrimeDtls();

                this.ViewState.Add("IsUserControlPostBack", true);

            }



            if (DataItem != null)
            {
                //   cbGender.ListGenderComboBox();

     
                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    BindYear();
                    BindComboEmpType();
                    BindComboInvestigation();
                    BindComboPenaltyLevel();
                    BindComboCrimeDtls();


                    lbl_Cmd_dtls_seq.Text = DataBinder.Eval(DataItem, "cmd_dtls_seq").ToString();
                  //  lblClassName.Text = DataBinder.Eval(DataItem, "cr_seq").ToString();

                    txtCrimeDtls.Text = DataBinder.Eval(DataItem, "crime_descr").ToString();
                    txtSection .Text  = DataBinder.Eval(DataItem, "crime_descr").ToString();
                    ComboYear.SelectedValue   = DataBinder.Eval(DataItem, "crime_year").ToString();
                   ComboCrimeDtls.SelectedValue = DataBinder.Eval(DataItem, "crd_seq").ToString();
                    ComboPenaltyLevel.SelectedValue = DataBinder.Eval(DataItem, "pen_level_seq").ToString();
                    string emp_seq = DataBinder.Eval(DataItem, "emp_seq").ToString();

                    BindEmp(emp_seq); 



                    ComboEmpType.Visible = false;
                    ComboInvestigation.Visible = false;
                    lbl1.Visible = false;
                    lbl2.Visible = false;
                    txtEmp.ReadOnly = true; 
        
             
                }


             

                btnUpdate.Visible = !(DataItem is Telerik.Web.UI.GridInsertionObject);
                btnAddnew.Visible = (DataItem is Telerik.Web.UI.GridInsertionObject);
            }


       
      
        }

        private void BindYear()
        {
            int myYearNow = DateTime.Today.Year + 543;
            ListYear(myYearNow - 10, myYearNow + 10);
            ComboYear.ClearSelection();
            ComboYear.Text = string.Empty;
        }

        public void ListYear(int MinYear, int MaxYear)
        {
            DataTable dt = new DataTable();
            DataColumn dc = dt.Columns.Add("Name");
        
            for (int i = MinYear; i <= MaxYear; i++)
            {
                dt.Rows.Add(Convert.ToString(i));
            }

            ComboYear.DataSource = dt;
            ComboYear.DataBind();

        }

         


        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }
        protected void btnAddnew_Click(object sender, EventArgs e)
        {

            Command_DisciplineBLL obj = new Command_DisciplineBLL();

            int cmd_seq = int.Parse (Session["cmd_seq"].ToString());
            int pen_level_seq = int.Parse(ComboPenaltyLevel.SelectedValue);
            string crime_descr, crime_section;
            int crime_year,cr_seq;
          
            string pos_ref, pos_name, class_name, job_site_name;
             double salary;

             class_name = lblClassName.Text;
             pos_name = lblPosName.Text;
             pos_ref = lblPosRef.Text;
             job_site_name = lblJobsiteName.Text;
             salary = Convert.ToDouble(lblSalary.Text);  

            crime_descr = txtCrimeDtls.Text;
            crime_section = txtSection.Text;
            crime_year = int.Parse(ComboYear.SelectedValue);
            cr_seq = int.Parse(ComboCrimeDtls.SelectedValue);  



           obj.InsertDisciplineCommandDetail(cmd_seq ,cr_seq ,int.Parse(lblEmpSeq.Text),
              pen_level_seq,pos_ref,pos_name,class_name,job_site_name,salary,crime_descr,crime_section,crime_year,"1"); 






 
        }


        protected void btnUpdate_Click(object sender, EventArgs e)
        {

                  int cmd_seq = int.Parse (Session["cmd_seq"].ToString());
            int pen_level_seq = int.Parse(ComboPenaltyLevel.SelectedValue);
            string crime_descr, crime_section;
            int crime_year,cr_seq,cmd_dtls_seq;
         

            crime_descr = txtCrimeDtls.Text;
            crime_section = txtSection.Text;
            crime_year = int.Parse(ComboYear.SelectedValue);
            cr_seq = int.Parse(ComboCrimeDtls.SelectedValue);

            cmd_dtls_seq = int.Parse(lbl_Cmd_dtls_seq.Text);

            obj.UpdateDisciplineCommandDetail(cmd_dtls_seq, cr_seq, pen_level_seq, crime_descr, crime_section, crime_year, "1");



        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }




        private IEmployeeBLL empBLL = new EmployeeBLL();



        private void BindEmp(string emp_seq)
        {

            EmployeeInfo infoEmp;
            IList<EmployeeInfo> infoList = empBLL.ListEmployees(emp_seq);
            infoEmp = infoList[0];


            // infoEmp.Class.Sequence != null

            lblEmpSeq.Text = infoEmp.Sequence.ToString();
            lblPosName.Text = lblPosName.Text = infoEmp.Position.Name != null ? infoEmp.Position.Name.ToString() : "";
            lblClassName.Text = infoEmp.Class.Name != null ? infoEmp.Class.Name.ToString() : "";
            lblJobsiteName.Text = infoEmp.WorkingJobSite.Name != null ? infoEmp.WorkingJobSite.Name.ToString() : "";
            lblSalary.Text = infoEmp.Salary != null ? infoEmp.Salary.ToString() : "";

            lblPosRef.Text = infoEmp.Position.RefNo != null ? infoEmp.Position.RefNo.ToString() : "";
            txtEmp.Text = infoEmp.Person.FirstName.ToString() + "   " + infoEmp.Person.LastName.ToString();

        }

        protected void btnFindEmp_Click(object sender, ImageClickEventArgs e)
        {


     

            string st = Hide.Value;

            if (st == "")
            {
                return;
            }

            EmployeeInfo infoEmp;
            IList<EmployeeInfo> infoList = empBLL.ListEmployees(st);
            infoEmp = infoList[0];


            // infoEmp.Class.Sequence != null

          lblEmpSeq.Text = infoEmp.Sequence.ToString();
            lblPosName.Text = lblPosName.Text = infoEmp.Position.Name != null ? infoEmp.Position.Name.ToString() : "";
            lblClassName.Text = infoEmp.Class.Name != null ? infoEmp.Class.Name.ToString() : "";
            lblJobsiteName.Text = infoEmp.WorkingJobSite.Name != null ? infoEmp.WorkingJobSite.Name.ToString() : "";
            lblSalary.Text = infoEmp.Salary != null ? infoEmp.Salary.ToString() : "";      

            lblPosRef.Text = infoEmp.Position.RefNo != null ? infoEmp.Position.RefNo.ToString() : "";
            txtEmp.Text = infoEmp.Person.FirstName.ToString() + "   " + infoEmp.Person.LastName.ToString();


        }


        private void BindComboInvestigation()
        {
            CommandDisciplineBLL obj = new CommandDisciplineBLL();
  DataTable dt;
  dt = obj.SelectInvestigationCombo();



  ComboInvestigation.DataValueField = "inv_seq";
  ComboInvestigation.DataTextField = "inv_name";

  ComboInvestigation.DataSource = dt;
  ComboInvestigation.DataBind(); 

        }

        private void BindComboPenaltyLevel()
        {
            CommandDisciplineBLL obj = new CommandDisciplineBLL();
            DataTable dt;
            dt = obj.SelectPenaltyLevelCombo();



            ComboPenaltyLevel.DataValueField = "pen_level_seq";
            ComboPenaltyLevel.DataTextField = "pen_level_name";

            ComboPenaltyLevel.DataSource = dt;
            ComboPenaltyLevel.DataBind();

        }



        private void BindComboCrimeDtls()
        {
            CommandDisciplineBLL obj = new CommandDisciplineBLL();
            DataTable dt;
            dt = obj.SelectCrimeDtlsCombo(); 



            ComboCrimeDtls.DataValueField = "crd_seq";
            ComboCrimeDtls.DataTextField = "crd_name";

            ComboCrimeDtls.DataSource = dt;
            ComboCrimeDtls.DataBind();

        }


        private void BindComboEmpType()
        {
            CommandDisciplineBLL obj = new CommandDisciplineBLL();
            DataTable dt;
            dt = obj.SelectEmpTypeCombo();

            ComboEmpType.DataValueField = "emp_type_seq";
            ComboEmpType.DataTextField = "emp_type_name";

            ComboEmpType.DataSource = dt;
            ComboEmpType.DataBind();

        }



        protected void btnFindEmp_Load(object sender, EventArgs e)
        {

            if (DataItem is Telerik.Web.UI.GridInsertionObject)
            {



             //   string INV_SEQ = Session["inv_seq"].ToString();

            
                //   btnFindEmp.Attributes.Add("OnClick", "LinkMDdetail(" + Hide.ClientID + "," + "'Forms/DialogEmp.aspx?ETID=" + ETID + "&JSID=1','../frmIframe.html',700,500)");
            //    btnFindEmp.Attributes.Add("OnClick", "LinkMDdetail(" + Hide.ClientID + "," + "'Forms/DialogCommandDiscipline.aspx?INV_SEQ=" + INV_SEQ + "','../frmIframe.html',700,500)");
            }

        }

        protected void ComboEmpType_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {

            Session["EmpType"] = ComboEmpType.SelectedValue;
          

        }

        protected void ComboInvestigation_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {

            Session["inv_seq"] = ComboInvestigation.SelectedValue;
            btnFindEmp.Visible = true;


            string INV_SEQ = Session["inv_seq"].ToString();
            string EMP_TYPT_SEQ = Session["emp_type_seq"].ToString(); 


            //   btnFindEmp.Attributes.Add("OnClick", "LinkMDdetail(" + Hide.ClientID + "," + "'Forms/DialogEmp.aspx?ETID=" + ETID + "&JSID=1','../frmIframe.html',700,500)");
            btnFindEmp.Attributes.Add("OnClick", "LinkMDdetail(" + Hide.ClientID + "," + "'Forms/DialogCommandDiscipline.aspx?INV_SEQ=" + INV_SEQ + "&EMP_TYPT_SEQ=" + EMP_TYPT_SEQ + "','../frmIframe.html',700,500)");



        }
    }
}